/**
 *
  path: '/quality',
  remark: "layout",
  name: '会诊质控',
  redirect: '/quality/qualitypre',
  hidden: false,
  meta: {
    title: '会诊质控',
    icon: 'fa fa-retweet'
  },
  children: [{
    path: 'qualitypre',
    name: '前质控',
    remark: "qualitypre",
    hidden: false,
    meta: {
      title: '前质控',
      icon: 'svg-hzqzk'
    }
  }
  * 遍历获取路由数组
 */
import store from '@/store'
const HTTP_KEY = 'HTTP_'
export default function menuCorrect(menuData) {
  console.log('menuCorrect')
  const PLen = menuData.length
  let menuArr = []
  let tagsArr = []
  if (menuData != null && PLen > 0) {
    let _objMain = { //示例：http://192.168.0.250:9214/kayimain/checksourceEnquiry（去菜单+头部） http://192.168.0.250:9214/check/sourceEnquiry
      path: '/kayimain',
      remark: 'layoutmain',
      name: 'kayimain',
      hidden: true,
      meta: {
        title: ''
      },
      children: []
    }
    for (var i = 0; i < PLen; i++) {
      console.log('menuData[i]', JSON.stringify(menuData[i]))
      const _obj = {}
      if (menuData[i].tags.length > 0 && menuData[i].nodes.length > 0) {
        if (menuData[i].text == '业务维护') {
          console.log('menuData[i]2', menuData[i])
        }
        _obj.path = getUrl(menuData[i].href)
        _obj.remark = 'layout'
        _obj.name = menuData[i].text
        _obj.redirect = menuData[i].href + '/' + menuData[i].nodes[0].href.split('|')[0]
        // _obj.hidden = !!menuData[i].status
        _obj.hidden = menuData[i].status == 1 ? true : false
        _obj.meta = {
          title: menuData[i].text,
          icon: menuData[i].img
        }

        tagsArr = tagsArr.concat(menuData[i].tags)
        _obj.children = []
        const _nodes = menuData[i].nodes
        const CLen = _nodes.length
        for (var j = 0; j < CLen; j++) {
          const _objChild = {}
          _objChild.path = getUrl(_nodes[j].href.split('|')[0])
          _objChild.remark = _nodes[j].href.split('|')[1]
          _objChild.name = _nodes[j].text
          _objChild.hidden = _nodes[j].status == 1 ? true : false
          _objChild.meta = {
            title: _nodes[j].text,
            icon: _nodes[j].img,
            iframeurl: _nodes[j].href.split('|')[2]
          }
          // _objChild.children = []
          tagsArr = tagsArr.concat(_nodes[j].tags)
          _obj.children.push(_objChild)
          if (_nodes[j].href.indexOf(HTTP_KEY) < 0 && _nodes[j].href.indexOf('http') !== 0) {
            const _objChild2 = JSON.parse(JSON.stringify(_objChild))
            _objChild2.hidden = true
            _objChild2.name = _objChild2.name + j
            _objChild2.path = _obj.path.replace('/', '') + _objChild2.path
            _objMain.children.push(_objChild2)
          }
        }
        menuArr.push(_obj)
      }
    }
    store.dispatch('user/resetTags', tagsArr)
    if (_objMain.children.length) {
      menuArr.push(_objMain)
    }
    console.log('menuArr', menuArr)
    return menuArr
  } else {
    store.dispatch('user/resetTags', [])
    return []
  }
}

function getUrl(url) {
  if (url.indexOf(HTTP_KEY) >= 0) {
    const str = url.replace(HTTP_KEY, '')
    const _url = str.split('##')
    if (_url.length > 1) {
      return window.g[_url[0]] ? window.g[_url[0]] + _url[1] : url
    }
    return window.g[_url[0]] ? window.g[_url[0]] : url
  } else {
    return url
  }
}
